import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class myThread extends JPanel implements Runnable{

    private static boolean suspend=false;
    Graphics g;
    Thread a,b;

    public void init()
    {
        a=new Thread(this);
        b=new Thread(this);
    }

    public void start()
    {
        g=getGraphics();
        if(g == null)
        {
            System.out.println("null");
        }
        try{
            a.start();
            b.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public synchronized void toResume()  {
        notify();
        suspend=false;

    }
    public void stop()
    {
        a.stop();
        b.stop();
    }


    @Override
    public void run() {

        System.out.println("线程启动");

        int s ;

          synchronized (this){


               while (true)
               {
                   while (suspend)
                   {
                       try {
                           wait();
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
                   s=50+Random.getRandomS();
                   g.setColor(Random.getRandomColor());
                   g.fillOval(100+Random.getRandomX(),100+Random.getRandomX(),s,s);
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }

    }

    public static void main(String[] args) {
        JFrame f =new JFrame("TR06_随机圆 04180625段煜锋");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1000,1000);
        JPanel p1 =new JPanel();

        JButton btn_start = new JButton("开始");
        JButton btn_Resume = new JButton("继续");
        JButton btn_Suspend = new JButton("暂停");
        JButton btn_end = new JButton("终止");
        JButton btn_close= new JButton("关闭窗口");

        btn_Resume.setBackground(Color.magenta);
        btn_Suspend.setBackground(Color.GREEN);
        btn_end .setBackground(Color.YELLOW);
        btn_start.setBackground(Color.RED);
        btn_close.setBackground(Color.CYAN);

        p1.setSize(800,200);
        f.add(p1);

        p1.add(btn_start);
        p1.add(btn_Resume);
        p1.add(btn_Suspend);
        p1.add(btn_end );
        p1.add(btn_close);

        btn_start.setEnabled(true);
        btn_Resume.setEnabled(false);
        btn_Suspend.setEnabled(false);
        btn_end.setEnabled(false);
        btn_close.setEnabled(false);

        myThread mt=new myThread();
        mt.init();
        f.add(mt);
        f.setVisible(true);


        //开始按钮
        btn_start.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mt.start();
                btn_start.setEnabled(false);
                btn_Suspend.setEnabled(true);
                btn_end.setEnabled(true);
            }
        });

       // 继续按钮
        btn_Resume.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e1) {

                if (suspend=true){
                   mt.toResume();
                   btn_Resume.setEnabled(false);
                   btn_Suspend.setEnabled(true);
                }
            }
        });
        //暂停按钮
        btn_Suspend.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e2) {
                suspend=true;
                btn_Resume.setEnabled(true);
                btn_Suspend.setEnabled(false);

            }
        });
        //终止按钮
        btn_end.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mt.stop();
                btn_close.setEnabled(true);
                btn_Resume.setEnabled(false);
                btn_Suspend.setEnabled(false);
                btn_end.setEnabled(false);

            }
        });
        btn_close.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f.dispose();
            }
        });

    }




}
